package com.example.demo2.service.impl;

import com.example.demo2.dao.TestMapper;
import com.example.demo2.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import static com.example.demo2.utils.HttpUtils.check;

@Slf4j
@Service
public class TestCase4ServiceImpl implements ITestCase4Service {


    @Autowired
    private TestMapper testMapper;
    @Autowired
    private ICheckService checkService;
    @Autowired
    private ITestCase4RequiresNewService testCase4RequiresNewService;
    @Autowired
    private ITestCase4SupportsService testCase4SupportsService;
    @Autowired
    private ITestCase4NotSupportedService testCase4NotSupportedService;
    @Autowired
    private ITestCase4NeverService testCase4NeverService;
    @Autowired
    private ITestCase4NestedService testCase4NestedService;

    //    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Transactional
    @Override
    public void requiresNew() {
        log.info("------------------------------------------------");
        testMapper.insert("case4");
        check();
//        checkService.check();
        log.info("此处表现正常，事务在结束后提交");

        testCase4RequiresNewService.case4RequiresNew();
        check();
        log.info("此时 4.1 的事务已经提交到数据库");
        checkService.check();

        testMapper.delete("case4.1");

        testMapper.delete("case4");
    }

    @Transactional
    @Override
    public void withTransactionalSupports() {
        testCase4SupportsService.withSupports();
    }

    @Override
    public void withoutTransactionalSupports() {
        testCase4SupportsService.withSupports();
    }

    @Transactional(propagation = Propagation.MANDATORY)
    @Override
    public void mandatory() {

    }

    @Transactional
    @Override
    public void notSupported() {
        testMapper.insert("case4.3.1");
        testCase4NotSupportedService.insertSomeData();
        testMapper.delete("case4.3.1");
        testMapper.delete("case4.3.2");
    }

    @Transactional
    @Override
    public void never() {
        testCase4NeverService.doNever();
    }

    @Transactional
    @Override
    public void nested() {
        testMapper.insert("case4.4.1");
        testCase4NestedService.insertAndCheck();
        check();
        log.info("在外面看，事务是没有被提交的，2条数据都没有插入数据库");
        checkService.check();
        log.info("在里面看，可以查到 case4.4.2, 说明这和 REQUIRED 是一模一样的");
        testMapper.delete("case4.4.1");
        testMapper.delete("case4.4.2");
    }

    @Transactional(propagation = Propagation.NESTED)
    @Override
    public void nestedWithNested() {
        testMapper.insert("case4.4.1");
        testCase4NestedService.insertAndCheck();
        check();
        log.info("在外面看，事务是没有被提交的，2条数据都没有插入数据库");
        checkService.check();
        log.info("在里面看，可以查到 case4.4.2, 说明这和 REQUIRED 是一模一样的");
        testMapper.delete("case4.4.1");
        testMapper.delete("case4.4.2");
    }

}
